import { getOrderCount, indexList } from "../../../api/order";
import { unicomSelect } from "../../../api";
const app = getApp();
Page({
  data: {

    user: {
      avatar: '',
      nickName: ''
    },
    numList: [
      {
        icon: '../../../images/order-icon1.png',
        title: '待付款',
        num: 0,
        id: 2
      },
      {
        icon: '../../../images/order-icon2.png',
        title: '待发货',
        num: 0,
        id: 10
      },
      {
        icon: '../../../images/order-icon3.png',
        title: '待收货',
        num: 0,
        id: 20
      },
      {
        icon: '../../../images/order-icon4.png',
        title: '租赁中',
        num: 0,
        id: 30
      }
    ],
    navList: [
      {
        icon: '../../../images/mine-icon1.png',
        title: '我的收藏',
        link: '../../../pagesA/pages/collect/collect'
      },
      {
        icon: '../../../images/mine-icon2.png',
        title: '我要买断',
        link: '../../../pagesA/pages/myOrder/myOrder?order_status=62'
      },
      {
        icon: '../../../images/mine-icon3.png',
        title: '我要归还',
        link: '../../../pagesA/pages/myOrder/myOrder?order_status=30'
      },
      {
        icon: '../../../images/mine-icon4.png',
        title: '我要还款',
        link: '../../../pagesA/pages/myOrder/myOrder?order_status=30'
      },
      {
        icon: '../../../images/service-Popup-icon1.png',
        title: '机构订单',
        link: '../../../pagesA/pages/offlineStore/offlineStore'
      },
      {
        icon: '../../../images/mine-iconb.png',
        title: '绑定银行卡',
        link: '../../../pagesA/pages/bankCard/bankCard'
      },
      // {
      //   icon: '../../../images/mine-icon5.png',
      //   title: '商家入驻',
      //   link: '../../../pagesA/pages/supplierApply/supplierApply'
      // },
      {
        icon: '../../../images/mine-icon7.png', 
        title: '帮助中心',
        link: '../../service/service'
      },
      {
        icon: '../../../images/mine-icon8.png',
        title: '投诉建议',
        link: '../../../pagesA/pages/complaints/complaints'
      }
      // {
      //   icon: '../../../images/service-Popup-icon1.png',
      //   title: '联通5G',
      //   link: '../../../pagesA/pages/unicom/unicom'
      // }
    ],
    recommendList: [],
    page: 1,
    pageSize: 8,
    total: 0,
    showLoading: false,
    phone_brand:'',
  },
  onLoad() {
  
  },
  onShow() {
    this.getInfo();
  },
  toWelfare() {
    my.navigateTo({
      url: ''
    })
  },
  goToSignIn() {
    my.navigateTo({
      url: '../../myOrder/myOrder?order_status=30'
    })
  },
  getInfo() {
    const token = my.getStorageSync({ key: 'token' }).data;
    const that = this;
    if (token) {
      const { avatar, nickName } = my.getStorageSync({ key: 'userInfo' }).data;
      that.setData({
        user: {
          avatar,
          nickName
        }
      })
      this.getOrderNum();
      this.getList();
    } else {
      my.stopPullDownRefresh();
      this.authUser()
    }
  },
  getList(flag) {
    // flag为true则加载更多
    const { page, pageSize } = this.data;
    indexList({
      page: flag ? page + 1 : 1,
      pageSize
    }).then(res => {
      let list = [...this.data.recommendList];
      if (flag) {
        this.setData({
          showLoading: false
        })
        list = [...list, ...res.data];
      } else {
        list = res.data;
      }
      this.setData({
        recommendList: list,
        page: res.current_page,
        total: res.total
      })
    })
  },
  getOrderNum() {
    getOrderCount().then(res => {
      my.stopPullDownRefresh();
      const list = [...this.data.numList];
      list[0].num = res.no_pay || 0;
      list[1].num = res.no_send || 0;
      list[2].num = res.no_delivery || 0;
      list[3].num = res.renting || 0;
      this.setData({
        numList: list
      });
    }).catch(err => {
      my.stopPullDownRefresh();
    })
  },
  toJumpDetails(e) {
    my.navigateTo({
      url: '../../productDetails/productDetails?goodsId=' + e.info.id+'&sale_user_id='+app.globalData.sale_user_id
    })
  },
  navItemClick(e) {
    const { link,index } = e.currentTarget.dataset
  
      if (link) {
        my.navigateTo({ url: link });
      }
  },
  authUser() {

    const that = this
    app.getAuthUser(function (res) {
      if (res) {
        let userInfo = my.getStorageSync({ key: 'userInfo' }).data;
        that.setData({
          user: userInfo
        })
        that.getOrderNum();
        that.getList();
      }
    })
  },
  checkOrder(e) {
    const token = my.getStorageSync({ key: 'token' }).data;
    const id = e.currentTarget.dataset.id;
    const that = this;
    if (token) {
      my.navigateTo({
        url: '../../../pagesA/pages/myOrder/myOrder?order_status=' + (id || 0)
      });
    } else {
      app.getAuthUser(function (res) {
        if (res) {
          const { avatar, nickName } = my.getStorageSync({ key: 'userInfo' }).data;
          that.setData({
            user: {
              avatar,
              nickName
            }
          });
          my.navigateTo({
            url: '../../myOrder/myOrder?order_status=' + (id || 0)
          });
        }
      })
    }
  },
  onReachBottom() {
    // 页面拉到底部
    const { recommendList, total } = this.data;
    if (recommendList.length < total) {
      this.setData({
        showLoading: true
      })
      this.getList(true);
    }
  },
  onPullDownRefresh() {
    this.getInfo();
  }
});
